package com.ruoyi.kenuo.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.RsaUtilClient;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.kenuo.domain.KernelFkReply;
import com.ruoyi.kenuo.service.IKernelFkReplyService;
import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.kenuo.domain.KernelPendiFk;
import com.ruoyi.kenuo.service.IKernelPendiFkService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 反馈Controller
 *
 * @author polo
 * @date 2022-12-05
 */
@Api(tags = "反馈")
@RestController
@RequestMapping("/kenuo/fk")
public class KernelPendiFkController extends BaseController
{
    @Autowired
    private IKernelPendiFkService kernelPendiFkService;
    @Autowired
    private IKernelFkReplyService kernelFkReplyService;
    /**
     * 查询反馈列表
     */
    //@PreAuthorize("@ss.hasPermi('kenuo:fk:list')")
    @GetMapping("/list")
    public TableDataInfo list(KernelPendiFk kernelPendiFk) throws Exception {
        String fkName = kernelPendiFk.getFkName();
        if(StringUtils.isNotEmpty(fkName)){
            fkName = RsaUtilClient.decryptByPrivateKeyTwo("",fkName);
            kernelPendiFk.setFkName(fkName);
        }
        String tel = kernelPendiFk.getTel();
        if(StringUtils.isNotEmpty(tel)){
            tel = RsaUtilClient.decryptByPrivateKeyTwo("",tel);
            kernelPendiFk.setTel(tel);
        }
        startPage();
        List<KernelPendiFk> list = kernelPendiFkService.selectKernelPendiFkList(kernelPendiFk);
        if(!list.isEmpty()){
            list.stream().forEach(x->{
                KernelFkReply kfreply = new KernelFkReply();
                kfreply.setRemarkOne(x.getId());
                List<KernelFkReply> plist = kernelFkReplyService.selectKernelFkReplyList(kfreply);
                x.setFkList(plist);
                String fkcuName = x.getFkName();
                String mtel = x.getTel();
                if(StringUtils.isNotEmpty(fkcuName)){
                    try {
                        fkcuName = RsaUtilClient.encryptByPublicKeyTwo(fkcuName);
                        x.setFkName(fkcuName);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
                if(StringUtils.isNotEmpty(mtel)){
                    try {
                        mtel = RsaUtilClient.encryptByPublicKeyTwo(mtel);
                        x.setTel(mtel);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            });
        }
        return getDataTable(list);
    }

    /**
     * 导出反馈列表
     */
    @PreAuthorize("@ss.hasPermi('kenuo:fk:export')")
    @Log(title = "反馈", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KernelPendiFk kernelPendiFk)
    {
        List<KernelPendiFk> list = kernelPendiFkService.selectKernelPendiFkList(kernelPendiFk);
        ExcelUtil<KernelPendiFk> util = new ExcelUtil<KernelPendiFk>(KernelPendiFk.class);
        util.exportExcel(response, list, "反馈数据");
    }

    /**
     * 获取反馈详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(kernelPendiFkService.selectKernelPendiFkById(id));
    }

    /**
     * 新增反馈
     */
    @Log(title = "反馈", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KernelPendiFk kernelPendiFk) throws Exception {
        String fkName = kernelPendiFk.getFkName();
        if(StringUtils.isNotEmpty(fkName)){
            fkName = RsaUtilClient.decryptByPrivateKeyTwo("",fkName);
            kernelPendiFk.setFkName(fkName);
        }
        String tel = kernelPendiFk.getTel();
        if(StringUtils.isNotEmpty(tel)){
            tel = RsaUtilClient.decryptByPrivateKeyTwo("",tel);
            kernelPendiFk.setTel(tel);
        }
        return toAjax(kernelPendiFkService.insertKernelPendiFk(kernelPendiFk));
    }

    /**
     * 修改反馈
     */
    @PreAuthorize("@ss.hasPermi('kenuo:fk:edit')")
    @Log(title = "反馈", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KernelPendiFk kernelPendiFk)
    {
        return toAjax(kernelPendiFkService.updateKernelPendiFk(kernelPendiFk));
    }

    /**
     * 删除反馈
     */
    @PreAuthorize("@ss.hasPermi('kenuo:fk:remove')")
    @Log(title = "反馈", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(kernelPendiFkService.deleteKernelPendiFkByIds(ids));
    }
}
